# Copyright 2008 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.ext import db
import datetime

class UTC(datetime.tzinfo):
  """
  A cheap hacky class that provides a tzinfo for datetime objects. ~JFisher
  """
  zero = datetime.timedelta(0)
  
  def utcoffset(self, dt):
    return self.zero
  
  def dst(self, dt):
    return self.zero

  def tzname(self, dt):
    return 'UTC'  

class User(db.Model):
  """
  Represents a user of the app, stores their location data, name, and picture
  """
  opensocial_id = db.StringProperty()
  latitude = db.StringProperty(default="")
  longitude = db.StringProperty(default="")
  thumbnail_url = db.StringProperty(default="")
  name = db.StringProperty(default="")
  updated = db.DateTimeProperty(auto_now = True)

  def toJson(self):
    updated = self.updated.replace(tzinfo = UTC())
    return { "name" : self.name.encode('utf-8'),
             "latitude" : self.latitude.encode('utf-8'),
             "longitude" : self.longitude.encode('utf-8'),
             "thumbnail" : self.thumbnail_url.encode('utf-8'),
             "id" : self.opensocial_id.encode('utf-8'),
             "updated" : updated.strftime("%a, %d %b %Y %H:%M:%S %Z%z") }
